package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartItemDTO;
import com.sky.dto.ShoppingCartItemDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCartItem;
import com.sky.entity.ShoppingCartItem;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addShoppingCart(ShoppingCartItemDTO shoppingCartItemDTO) {
        //DTO里没有用户id数据，所以要想办法搞到（借助ThreadLocal）
        ShoppingCartItem shoppingCartItem0 = new ShoppingCartItem();
        BeanUtils.copyProperties(shoppingCartItemDTO,shoppingCartItem0);
        shoppingCartItem0.setUserId(BaseContext.getCurrentId());//todo 这里这个id为什么是user_id???
        log.info("输出这里的获得的用户id，{}",shoppingCartItem0.getUserId());//todo 用户id为空
        //根据用户id查询该用户的购物车数据，查看所有商品项，返回到一个list中
        List<ShoppingCartItem> list=shoppingCartMapper.list(shoppingCartItem0);
        //如果当前购物车有商品
        if(list!=null && !list.isEmpty()){
            //此时拿到的是list
            ShoppingCartItem cartItem = list.get(0);
            //商品数量改变
            cartItem.setNumber(cartItem.getNumber()+1);
            //后端改了还要在数据库中改才是真的改
            shoppingCartMapper.updateNumberById(cartItem);
        }else{
            //没有则插入一条
            //还要分插入的是菜品还是套餐
            if(shoppingCartItemDTO.getDishId()!=null){//插入的是菜品
                //还要插入name、amount、image
                Dish dish = dishMapper.getDishById(shoppingCartItem0.getDishId());
                shoppingCartItem0.setName(dish.getName());
                shoppingCartItem0.setAmount(dish.getPrice());
                shoppingCartItem0.setImage(dish.getImage());
            }else{
                Setmeal setmeal = setmealMapper.getById(shoppingCartItem0.getSetmealId());
                shoppingCartItem0.setName(setmeal.getName());
                shoppingCartItem0.setImage(setmeal.getImage());
                shoppingCartItem0.setAmount(setmeal.getPrice());
            }
            shoppingCartItem0.setNumber(1);
            shoppingCartItem0.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCartItem0);//这里不单单是dishId、setmealId、dishFlavor这三个属性，是要负责在数据库中真正的改，所以插入的是全部属性
        }
    }

    //完成查看购物车接口
    @Override
    public List<ShoppingCartItem> showShoppingCart() {
        ShoppingCartItem shoppingCartItem = new ShoppingCartItem();
        shoppingCartItem.setUserId(BaseContext.getCurrentId());
        //根据用户id查看购物车
        List<ShoppingCartItem> list =shoppingCartMapper.showShoppingCart(shoppingCartItem);
        return list;
    }

    @Override
    public void cleanShoppingCart() {
        ShoppingCartItem shoppingCartItem = new ShoppingCartItem();
        shoppingCartItem.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.cleanShoppingCart(shoppingCartItem);
    }
}
